#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os

from pjdog_public import load_record, write_record, load_path_files
from pjdog_func import *


class PJDogDeal(object):
    def __init__(self, config):
        self.config = config
        logging.info('config {0}'.format(config))

    # 处理线程 读取任务
    def run(self):
        deals = load_record(self.config.record_file)
        file_list = load_path_files(self.config.data_path)
        for file_name in file_list:
            if os.path.exists(file_name):
                size = os.stat(file_name).st_size
                if size < self.config.min_size:
                    logging.warning('file {0} invalid'.format(file_name))
                    continue
            if file_name in deals:
                continue
            logging.info(file_name)
            func = eval(self.config.task_type)
            status = func(file_name, self.config)
            if status == 0:
                write_record(self.config.record_file, file_name)
